home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Commentator / sieve.c < prev   
C/C++ Source or Header  |  2000-06-23  |  1KB  |  38 lines

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <time.h>
  4.  
  5. #define TEST(f,x)    (*(f+(x)/16)&(1<<(((x)%16L)/2)))
  6. #define SET(f,x)    *(f+(x)/16)|=1<<(((x)%16L)/2)
  7.  
  8. void main(int argc, char *argv[])
  9. {
  10.   unsigned char *feld=NULL, *zzz;
  11.   unsigned long teste=1, max, mom, hits=1, count, alloc, s=0, e=1;
  12.  
  13.   if (argc > 1)
  14.     max = atol (argv[1]) + 10000;
  15.   else
  16.     max = 14010000L;
  17.  
  18.   while (feld==NULL)
  19.         zzz = feld = malloc (alloc=(((max-=10000L)>>4)+1L));
  20.  
  21.   for (count=0; count<alloc; count++) *zzz++ = 0x00;
  22.  
  23.   while ((teste+=2) < max)
  24.         if (!TEST(feld, teste)) {
  25.                 if  (++hits%2000L==0) {printf (" %ld. prime number\x0d", hits); fflush(stdout);}
  26.                 for (mom=3L*teste; mom<max; mom+=teste<<1) SET (feld, mom);
  27.                 }
  28.  
  29.   while (s<e) {
  30.         printf ("\n\nStart of Area : "); fflush (stdout); scanf ("%ld", &s);
  31.         printf ("End   of Area : ");     fflush (stdout); scanf ("%ld", &e);
  32.  
  33.         count=s-2; if (s%2==0) count++;
  34.         while ((count+=2)<e) if (!TEST(feld,count)) printf ("%ld\t", count);
  35.         }
  36.   free (feld);
  37. }
  38.